<?php
// Copyright 2012 @Wondershare
// $Id: SiteController.php 437343 2012-08-04 07:16:28Z zhangqian $

class SiteController extends Controller {
	/**
	 * Declares class-based actions.
	 */
	public function actions() {
		return array (
				// captcha action renders the CAPTCHA image displayed on the
				// contact page
				'captcha' => array (
						'class' => 'CCaptchaAction',
						'backColor' => 0xFFFFFF 
				),
				// page action renders "static" pages stored under
				// 'protected/views/site/pages'
				// They can be accessed via: index.php?r=site/page&view=FileName
				'page' => array (
						'class' => 'CViewAction' 
				) 
		);
	}
	
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex() {
		// echo "Adsfasdf";exit();
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
	/* 	$cur_domain = $_SERVER['HTTP_HOST'];
$cookie_domain = preg_replace("/([^\.]+)\.(.+)/i",'$2',$cur_domain);
defined('COOKIE_DOMAIN') or define('COOKIE_DOMAIN',$cookie_domain);
print_r($cookie_domain); */
		$this->render ( 'index' );
	}
	public function actionLogin_out() {
		// echo "Adsfasdf";exit();
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		unset(Yii::app()->session['username']);
		unset(Yii::app()->session['password']);
		unset(Yii::app()->session['email']);
		unset(Yii::app()->session['user_id']);
		$this->redirect ( $this->createUrl ( "/site/index" ) );
	}
	public function actionUpdate() {
		// echo "Adsfasdf";exit();
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$username=Yii::app()->session['username'];
		$user=Users::model()->find ( "login_name=?", array($username));
		$password=$_POST['new_password'];
		$salt=$user->salt;
		$user->password=md5(md5($password).$salt);
		$user->save();
		$user=Users::model()->find ( "login_name=?", array($username));
		$this->redirect ( $this->createUrl ( "/site/index" ) );
	}
	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError() {
		if ($error = Yii::app ()->errorHandler->error) {
			if (Yii::app ()->request->isAjaxRequest)
				echo $error ['message'];
			else
				$this->render ( 'error', $error );
		}
	}
	
	/**
	 * Displays the contact page
	 */
	public function actionContact() {
		$model = new ContactForm ();
		if (isset ( $_POST ['ContactForm'] )) {
			$model->attributes = $_POST ['ContactForm'];
			if ($model->validate ()) {
				$headers = "From: {$model->email}\r\nReply-To: {$model->email}";
				mail ( Yii::app ()->params ['adminEmail'], $model->subject, $model->body, $headers );
				Yii::app ()->user->setFlash ( 'contact', 'Thank you for contacting us. We will respond to you as soon as possible.' );
				$this->refresh ();
			}
		}
		$this->render ( 'contact', array (
				'model' => $model 
		) );
	}
	
	/**
	 * Displays the login page
	 */
	public function actionLogin() {
		$model = new LoginForm ();
		// echo Yii::app()->session['username'];exit();
		// if it is ajax validation request
		if (isset ( $_POST ['ajax'] ) && $_POST ['ajax'] === 'login-form') {
			echo CActiveForm::validate ( $model );
			Yii::app ()->end ();
		}
		
		// collect user input data
		if (isset ( $_POST ['LoginForm'] )) {
			// print_r($_POST['LoginForm']);exit();
			$model->attributes = $_POST ['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if ($model->validate () && $model->login ()) {
				if (isset ( $_GET ['redirect'] ))
					// $this->redirect($_GET['redirect']);
					header ( "location:{$_GET['redirect']}" );
				else {
					
					$this->redirect ( $this->createUrl ( "/site/index" ) );
				
				}
			}
		}
		// display the login form
		$this->render ( 'login', array (
				'model' => $model 
		) );
	}
	
	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionRegister() {
		$user = new Users();
		if(isset($_POST['Username'])){
		$user->login_name=$_POST['Username'];
		$user->password=$_POST['Password'];
		$user->email=$_POST['Email'];
		$register_status=UserIdentity::register($user->login_name, $user->password, $user->email);
		if ($register_status=="register success")
		{
			$model = new LoginForm ();
			$model->username=$user->login_name;
			$model->password=$user->password;
			$model->rememberMe=1;
			if ($model->validate () && $model->login ()) {
				//$info=Yii::app()->session['username'];
				$this->redirect ( $this->createUrl ( "/site/index" ) );
			}
		}
		}
	}
	/**
	 *
	 */
	/* public function actionGet_password() {
		$model = new Users ();
		if (isset ( $_POST ['Users'] )) {
			$model->attributes = $_POST ['Users'];
			$rst = Users::model ()->find ( "login_name=? and email=?", array (
					$model->login_name,
					$model->email 
			) );
			if ($rst) {
				$password = $rst->password;
				// 添加发送邮件的功能
				// header("location:http://www.hao123.com");
				// echo phpinfo();exit();
				$mymail = new mail ();
				$mymail->send_mail ( "497688075@qq.com", "just test!", "<b>your password is </b>{$password}......" );
				echo "mail success!!";
				$this->redirect ( $_GET ['redirect'] );
			} else
				echo "error username or error email!!";
		}
		$this->render ( 'get_password', array (
				'model' => $model 
		) );
	} */
	public function actionLogin_ajax()
	
	{
		if(Yii::app()->request->isAjaxRequest)
	
		{
			$info="undefined infomation";
			//$username='asdfasd';
			$username = Yii::app()->request->getParam('username');
			$password=	Yii::app()->request->getParam('password');
			//$val =  Yii::app()->request->getParam('val');
			$model = new LoginForm ();
			$model->username=$username;
			$model->password=$password;
			$model->rememberMe=1;
			if ($model->validate () && $model->login ()) {
				//$info=Yii::app()->session['username'];
				$info="login success";
			}
			else
			{
				$user=Users::model()->find("login_name=?",array($username));
				$info=($user==null)?"User does not exist":"Password error";
			}
			echo CJSON::encode(array('info'=>$info));
	
		}
	
	}
	public function actionRegister_ajax()
	
	{
		if(Yii::app()->request->isAjaxRequest)
	
		{
			//$username='asdfasd';
			if($_GET['var']=='username')
			{
			$username = Yii::app()->request->getParam('Username');
			$user=Users::model()->find ( "login_name=?", array($username));
			$info=($user==null)?true:false;
			echo CJSON::encode($info);
			}
			if($_GET['var']=='email')
			{
				$email = Yii::app()->request->getParam('Email');
				$user=Users::model()->find("email=?", array($email));
				$info=($user==null)?true:false;
				echo CJSON::encode($info);
			}
			if($_GET['var']=='code')
			{
				$code=$_GET['Veryfy_Code'];
				$info=Authcode::check($code);
				echo CJSON::encode($info);
			}
			if($_GET['var']=='password')
			{
				$username=Yii::app()->session['username'];
				$user=Users::model()->find ( "login_name=?", array($username));
				$password=$user->password;
				$salt=$user->salt;
				$old_password=Yii::app()->request->getParam('old_password');
				$info=$password==md5(md5($old_password).$salt)?true:false;
				//$info=$password.'asdf';
				echo CJSON::encode($info);
			}
		}
	
	}
	public function actionGet_picture()
	{
		Authcode::show();
	}
	public function actionRefresh_picture()
	{	
		Authcode::show();
	}
}